package com.gwcm.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gwcm.exception.BusinessException;
import com.gwcm.mapper.CategoryMapper;
import com.gwcm.mapper.ProductMapper;
import com.gwcm.model.dto.product.ProductQueryRequest;
import com.gwcm.model.entity.Category;
import com.gwcm.model.entity.Product;
import com.gwcm.model.vo.ProductVo;
import com.gwcm.service.ProductService;
import com.gwcm.util.PageUtil;
import com.yhz.commonutil.constant.SortConstant;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author 15375
* @description 针对表【product】的数据库操作Service实现
* @createDate 2023-03-11 15:41:21
*/
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Resource
    private ProductMapper productMapper;
    @Resource
    private CategoryMapper categoryMapper;
    /**
     * 产品分页查询
     * @param productQueryRequest 查询条件
     * @return
     * @throws BusinessException
     */
    @Override
    public IPage<ProductVo> pageList(ProductQueryRequest productQueryRequest) throws BusinessException {
        IPage<Product> page = PageUtil.vaildPageParam(productQueryRequest.getCurrent(), productQueryRequest.getPageSize());
        String sortField = productQueryRequest.getSortField();
        String sortOrder = productQueryRequest.getSortOrder();
        String categoryName = productQueryRequest.getCategoryName();
        if (!StrUtil.isBlank(categoryName)) {
            QueryWrapper<Category> categoryQueryWrapper = new QueryWrapper<>();
            categoryQueryWrapper.like("category_name", categoryName);
            Category category = categoryMapper.selectOne(categoryQueryWrapper);
            productQueryRequest.setCategoryId(category.getId());
        }
        if (StrUtil.isBlank(sortOrder) || !StrUtil.equalsAny(sortOrder, SortConstant.SORT_ORDER_ASC, SortConstant.SORT_ORDER_DESC)) {
            //默认按升序排序
            sortOrder = SortConstant.SORT_ORDER_ASC;
        }
        productQueryRequest.setSortField(sortField);
        productQueryRequest.setSortOrder(sortOrder);
        System.out.println(productQueryRequest);
        IPage<ProductVo> productToIPage=productMapper.selectProductToPage(page, productQueryRequest);
        IPage<ProductVo> productVoIPage = productToIPage.convert(item -> {
            ProductVo productVo = new ProductVo();
            BeanUtils.copyProperties(item, productVo);
            return productVo;
        });

        return productVoIPage;
    }

    @Override
    public Product getProductByProductSn(String productSn) {
        return productMapper.getProductBySn(productSn);
    }

    @Override
    public List<Product> listAllProduct() {
        return productMapper.listAllProduct();
    }

    @Override
    public Product selectProductById(Long productId) {
        return productMapper.selectProductById(productId);
    }

    @Override
    public List<Product> selectProductList(Product product) {
        return productMapper.selectProductList(product);
    }

    @Override
    public int insertProduct(Product product) {
        product.setCreateTime(new Date());
        return productMapper.insertProduct(product);
    }

    @Override
    public int updateProduct(Product product) {
        product.setUpdateTime(new Date());
        return productMapper.updateProduct(product);
    }

    @Override
    public int deleteProductById(Long productId) {
        return productMapper.deleteProductById(productId);
    }
}




